package com.zh.webflux.config;

import com.zh.webflux.log.OperatingLogAspect;
import com.zh.webflux.log.OperationLogRecorder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * 文件服务minio配置
 * @author Zhou
 * @date 15:59 2022/12/8
 */
@Configuration
public class CommonConfig {

    /**
     * 处理跨域 crossDomain
     * @return corsFilter
     */
    @Order(Ordered.HIGHEST_PRECEDENCE)
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.addAllowedOriginPattern("*");
        config.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }

    /**
     * 记录操作日志
     */
    @Bean
    public OperatingLogAspect operatingLogAspect(OperationLogRecorder operationLogRecorder){
        return new OperatingLogAspect(operationLogRecorder);
    }


}
